package com.jiawa.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiawa.train.business.domain.TrainCarriage;
import com.jiawa.train.business.domain.TrainCarriageExample;
import com.jiawa.train.business.domain.TrainSeat;
import com.jiawa.train.business.domain.TrainSeatExample;
import com.jiawa.train.business.enums.SeatColEnum;
import com.jiawa.train.business.mapper.TrainCarriageMapper;
import com.jiawa.train.business.mapper.TrainSeatMapper;
import com.jiawa.train.business.req.TrainSeatQueryReq;
import com.jiawa.train.business.req.TrainSeatSaveReq;
import com.jiawa.train.business.resp.TrainSeatQueryResp;
import com.jiawa.train.common.exception.BusinessException;
import com.jiawa.train.common.exception.BusinessExceptionEnum;
import com.jiawa.train.common.resp.PageResp;
import com.jiawa.train.common.util.SnowUtil;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TrainSeatService {

    private static final Logger LOG = LoggerFactory.getLogger(TrainSeatService.class);

    @Resource
    private TrainSeatMapper trainSeatMapper;

    @Resource
    private TrainCarriageMapper trainCarriageMapper;


    public TrainSeat save(TrainSeatSaveReq req) {


        TrainSeat trainSeat = BeanUtil.copyProperties(req, TrainSeat.class);
        DateTime now = DateTime.now();
        trainSeat.setUpdateTime(now);

        if (ObjectUtil.isNotNull(req.getId())) {
            trainSeat.setUpdateTime(now);
            trainSeatMapper.updateByPrimaryKeySelective(trainSeat);
        } else {
            trainSeat.setId(SnowUtil.getSnowflakeNextId());
            trainSeat.setCreateTime(now);
            trainSeat.setUpdateTime(now);
            trainSeatMapper.insert(trainSeat);
        }
        return trainSeat;
    }


    public void delete(Long id) {
        TrainSeat trainSeat = trainSeatMapper.selectByPrimaryKey(id);
        if (trainSeat == null) {
            throw new BusinessException(BusinessExceptionEnum.PASSENGER_NOT_EXIST);
        }
        trainSeatMapper.deleteByPrimaryKey(id);
    }

    public PageResp<TrainSeatQueryResp> queryList(TrainSeatQueryReq req) {
        TrainSeatExample trainSeatExample = new TrainSeatExample();
        TrainSeatExample.Criteria criteria = trainSeatExample.createCriteria();
        if (ObjectUtil.isNotEmpty(req.getTrainCode())) {
            criteria.andTrainCodeEqualTo(req.getTrainCode());
        }

        PageHelper.startPage(req.getPage(), req.getSize());
        List<TrainSeat> trainSeatList = trainSeatMapper.selectByExample(trainSeatExample);

        PageInfo<TrainSeat> pageInfo = new PageInfo<>(trainSeatList);

        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());

        List<TrainSeatQueryResp> list = BeanUtil.copyToList(trainSeatList, TrainSeatQueryResp.class);

        PageResp<TrainSeatQueryResp> pageResp = new PageResp<>();
        pageResp.setList(list);
        pageResp.setTotal(pageInfo.getTotal());
        return pageResp;
    }


    public void genTrainSeat(String trainCode) {
        TrainCarriageExample trainCarriageExample = new TrainCarriageExample();
        trainCarriageExample.createCriteria().andTrainCodeEqualTo(trainCode);
        List<TrainCarriage> trainCarriageList = trainCarriageMapper.selectByExample(trainCarriageExample);

        if (trainCarriageList.isEmpty()) {
            throw new BusinessException(BusinessExceptionEnum.TRAIN_NOT_EXIST);
        }

        // 清空当前车次的座位信息
        TrainSeatExample trainSeatExample = new TrainSeatExample();
        trainSeatExample.createCriteria().andTrainCodeEqualTo(trainCode);
        trainSeatMapper.deleteByExample(trainSeatExample);

        for (TrainCarriage trainCarriage : trainCarriageList) {

            Integer carriageIndex = trainCarriage.getIndex();
            String seatType = trainCarriage.getSeatType();
            Integer rowCount = trainCarriage.getRowCount();
            Integer colCount = trainCarriage.getColCount();
            List<SeatColEnum> colList = SeatColEnum.getColsByType(seatType);

            for (int row = 1; row <= rowCount; row++) {

                for (int col = 0; col < colCount; col++) {
                    TrainSeat trainSeat = new TrainSeat();
                    trainSeat.setId(SnowUtil.getSnowflakeNextId());
                    trainSeat.setTrainCode(trainCode);
                    trainSeat.setCarriageIndex(carriageIndex);
                    trainSeat.setRow(String.valueOf(row));
                    trainSeat.setCol(colList.get(col).getCode());
                    trainSeat.setSeatType(trainCarriage.getSeatType());
                    trainSeat.setCarriageSeatIndex((row - 1) * colCount + col + 1);
                    trainSeat.setCreateTime(DateTime.now());
                    trainSeat.setUpdateTime(DateTime.now());
                    trainSeatMapper.insert(trainSeat);
                    LOG.info("生成座位：车次{} 车厢{} 座位{}", trainCode, carriageIndex, trainSeat.getRow() + trainSeat.getCol());
                }

            }

        }

    }

}
